home *** CD-ROM | disk | FTP | other *** search
/ Isometric Game Programming with DirectX 7.0 / Isometric Game Programming.iso / source / chapter17 / isohex17_2 / isohex17_2.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-08-19  |  10.7 KB  |  425 lines

  1. /*****************************************************************************
  2. IsoHex17_1.cpp
  3. Ernest S. Pazera
  4. 17AUG2000
  5. Start a WIN32 Application Workspace, add in this file
  6. Requires the following libs:
  7. ddraw.lib, dxguid.lib
  8. Requires the following files:
  9. DDFuncs.h.cpp, GDICanvas.h/cpp, IsoMouseMap.h/cpp,
  10. IsoScroller.h/cpp, IsoTilePlotter.h/cpp, IsoTileWalker.h/cpp
  11. TileSet.h/cpp. IsoHexCore.h, IsoHexDefs.h
  12. *****************************************************************************/
  13.  
  14. //////////////////////////////////////////////////////////////////////////////
  15. //INCLUDES
  16. //////////////////////////////////////////////////////////////////////////////
  17. #define WIN32_LEAN_AND_MEAN  
  18.  
  19. #include <windows.h>  
  20. #include "DDFuncs.h"
  21. #include "TileSet.h"
  22. #include "IsoHexCore.h"
  23. #include "IsoRenderer.h"
  24.  
  25.  
  26. //////////////////////////////////////////////////////////////////////////////
  27. //DEFINES
  28. //////////////////////////////////////////////////////////////////////////////
  29. //name for our window class
  30. #define WINDOWCLASS "ISOHEX17"
  31. //title of the application
  32. #define WINDOWTITLE "IsoHex 17-2"
  33.  
  34. const int MAPWIDTH=200;
  35. const int MAPHEIGHT=400;
  36.  
  37. //////////////////////////////////////////////////////////////////////////////
  38. //PROTOTYPES
  39. //////////////////////////////////////////////////////////////////////////////
  40. bool Prog_Init();//game data initalizer
  41. void Prog_Loop();//main game loop
  42. void Prog_Done();//game clean up
  43.  
  44. //////////////////////////////////////////////////////////////////////////////
  45. //GLOBALS
  46. //////////////////////////////////////////////////////////////////////////////
  47. HINSTANCE hInstMain=NULL;//main application handle
  48. HWND hWndMain=NULL;//handle to our main window
  49.  
  50. //directdraw
  51. LPDIRECTDRAW7 lpdd=NULL;
  52. LPDIRECTDRAWSURFACE7 lpddsMain=NULL;
  53. LPDIRECTDRAWSURFACE7 lpddsBack=NULL;
  54. LPDIRECTDRAWSURFACE7 lpddsFrame=NULL;
  55.  
  56. //tilesets
  57. CTileSet tsBack;//background
  58. CTileSet tsShadow;//tree shadow
  59. CTileSet tsTree;//tree foreground
  60. CTileSet tsCursor;//cursor
  61.  
  62. //isohexcore components
  63. CTilePlotter TilePlotter;//plotter
  64. CTileWalker TileWalker;//walker
  65. CScroller Scroller;//scroller
  66. CMouseMap MouseMap;//mousemap
  67. CRenderer Renderer;//renderer
  68.  
  69. POINT ptCursor;//keep track of the cursor
  70. POINT ptScroll;//keep track of how quickly we scroll
  71. bool bClick=false;
  72.  
  73. int iMap[MAPWIDTH][MAPHEIGHT];//map array(0=no tree, 1=tree)
  74.  
  75. //rendering functionprototype
  76. void RenderFunc(LPDIRECTDRAWSURFACE7 lpddsDst,RECT* rcClip,int xDst,int yDst,int xMap,int yMap);
  77.  
  78.  
  79. //////////////////////////////////////////////////////////////////////////////
  80. //WINDOWPROC
  81. //////////////////////////////////////////////////////////////////////////////
  82. LRESULT CALLBACK TheWindowProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
  83. {
  84.     //which message did we get?
  85.     switch(uMsg)
  86.     {
  87.     case WM_KEYDOWN:
  88.         {
  89.             switch(wParam)
  90.             {
  91.             case VK_ESCAPE:
  92.                 {
  93.                     DestroyWindow(hWndMain);
  94.                     return(0);
  95.                 }break;
  96.             }
  97.         }break;
  98.     case WM_LBUTTONDOWN:
  99.         {
  100.             //register a click
  101.             bClick=true;
  102.             return(0);
  103.         }break;
  104.     case WM_MOUSEMOVE:
  105.         {
  106.             //grab mouse x and y
  107.             int x=LOWORD(lParam);
  108.             int y=HIWORD(lParam);
  109.  
  110.             //reset scrolling speeds to zero
  111.             ptScroll.x=0;
  112.             ptScroll.y=0;
  113.  
  114.             //left scroll?
  115.             if(x<8) ptScroll.x=x-8;
  116.  
  117.             //upward scroll?
  118.             if(y<8) ptScroll.y=y-8;
  119.  
  120.             //right scroll?
  121.             if(x>=632) ptScroll.x=x-632;
  122.  
  123.             //downward scroll?
  124.             if(y>=472) ptScroll.y=y-472;
  125.         }break;
  126.     case WM_DESTROY://the window is being destroyed
  127.         {
  128.  
  129.             //tell the application we are quitting
  130.             PostQuitMessage(0);
  131.  
  132.             //handled message, so return 0
  133.             return(0);
  134.  
  135.         }break;
  136.     case WM_PAINT://the window needs repainting
  137.         {
  138.             //a variable needed for painting information
  139.             PAINTSTRUCT ps;
  140.             
  141.             //start painting
  142.             HDC hdc=BeginPaint(hwnd,&ps);
  143.  
  144.             /////////////////////////////
  145.             //painting code would go here
  146.             /////////////////////////////
  147.  
  148.             //end painting
  149.             EndPaint(hwnd,&ps);
  150.                         
  151.             //handled message, so return 0
  152.             return(0);
  153.         }break;
  154.     }
  155.  
  156.     //pass along any other message to default message handler
  157.     return(DefWindowProc(hwnd,uMsg,wParam,lParam));
  158. }
  159.  
  160.  
  161. //////////////////////////////////////////////////////////////////////////////
  162. //WINMAIN
  163. //////////////////////////////////////////////////////////////////////////////
  164. int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)
  165. {
  166.     //assign instance to global variable
  167.     hInstMain=hInstance;
  168.  
  169.     //create window class
  170.     WNDCLASSEX wcx;
  171.  
  172.     //set the size of the structure
  173.     wcx.cbSize=sizeof(WNDCLASSEX);
  174.  
  175.     //class style
  176.     wcx.style=CS_OWNDC | CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
  177.  
  178.     //window procedure
  179.     wcx.lpfnWndProc=TheWindowProc;
  180.  
  181.     //class extra
  182.     wcx.cbClsExtra=0;
  183.  
  184.     //window extra
  185.     wcx.cbWndExtra=0;
  186.  
  187.     //application handle
  188.     wcx.hInstance=hInstMain;
  189.  
  190.     //icon
  191.     wcx.hIcon=LoadIcon(NULL,IDI_APPLICATION);
  192.  
  193.     //cursor
  194.     wcx.hCursor=LoadCursor(NULL,IDC_ARROW);
  195.  
  196.     //background color
  197.     wcx.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);
  198.  
  199.     //menu
  200.     wcx.lpszMenuName=NULL;
  201.  
  202.     //class name
  203.     wcx.lpszClassName=WINDOWCLASS;
  204.  
  205.     //small icon
  206.     wcx.hIconSm=NULL;
  207.  
  208.     //register the window class, return 0 if not successful
  209.     if(!RegisterClassEx(&wcx)) return(0);
  210.  
  211.     //create main window
  212.     hWndMain=CreateWindowEx(0,WINDOWCLASS,WINDOWTITLE, WS_POPUP | WS_VISIBLE,0,0,320,240,NULL,NULL,hInstMain,NULL);
  213.  
  214.     //error check
  215.     if(!hWndMain) return(0);
  216.  
  217.     //if program initialization failed, then return with 0
  218.     if(!Prog_Init()) return(0);
  219.  
  220.     //message structure
  221.     MSG msg;
  222.  
  223.     //message pump
  224.     for(;;)    
  225.     {
  226.         //look for a message
  227.         if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
  228.         {
  229.             //there is a message
  230.  
  231.             //check that we arent quitting
  232.             if(msg.message==WM_QUIT) break;
  233.             
  234.             //translate message
  235.             TranslateMessage(&msg);
  236.  
  237.             //dispatch message
  238.             DispatchMessage(&msg);
  239.         }
  240.  
  241.         //run main game loop
  242.         Prog_Loop();
  243.     }
  244.     
  245.     //clean up program data
  246.     Prog_Done();
  247.  
  248.     //return the wparam from the WM_QUIT message
  249.     return(msg.wParam);
  250. }
  251.  
  252. //////////////////////////////////////////////////////////////////////////////
  253. //INITIALIZATION
  254. //////////////////////////////////////////////////////////////////////////////
  255. bool Prog_Init()
  256. {
  257.     //create IDirectDraw object
  258.     lpdd=LPDD_Create(hWndMain,DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN | DDSCL_ALLOWREBOOT);
  259.  
  260.     //set display mode
  261.     lpdd->SetDisplayMode(640,480,16,0,0);
  262.  
  263.     //create primary surface
  264.     lpddsMain=LPDDS_CreatePrimary(lpdd,1);
  265.  
  266.     //get back buffer
  267.     lpddsBack=LPDDS_GetSecondary(lpddsMain);
  268.  
  269.     //create the frame buffer
  270.     lpddsFrame=LPDDS_CreateOffscreen(lpdd,640,480);
  271.  
  272.     //load in the mousemap
  273.     MouseMap.Load("MouseMap.bmp");
  274.  
  275.     //set up the tile plotter
  276.     TilePlotter.SetMapType(ISOMAP_DIAMOND);//diamond mode
  277.     TilePlotter.SetTileSize(MouseMap.GetWidth(),MouseMap.GetHeight());//grab width and height from mousemap
  278.  
  279.     //set up tile walker to diamond mode
  280.     TileWalker.SetMapType(ISOMAP_DIAMOND);
  281.  
  282.     //set up screeen space
  283.     RECT rcTemp;
  284.     SetRect(&rcTemp,0,0,640,480);
  285.     Scroller.SetScreenSpace(&rcTemp);
  286.  
  287.     //load in tiles and cursor
  288.     tsBack.Load(lpdd,"backgroundts.bmp");
  289.     tsShadow.Load(lpdd,"treeshadowts.bmp");
  290.     tsTree.Load(lpdd,"treets.bmp");
  291.     tsCursor.Load(lpdd,"cursor.bmp");
  292.  
  293.     //grab tile extent from tileset
  294.     CopyRect(&rcTemp,&tsBack.GetTileList()[0].rcDstExt);
  295.  
  296.     //calculate the worldspace
  297.     Scroller.CalcWorldSpace(&TilePlotter,&rcTemp,MAPWIDTH,MAPHEIGHT);
  298.  
  299.     //calculate the mousemap reference point
  300.     MouseMap.CalcReferencePoint(&TilePlotter,&rcTemp);
  301.  
  302.     //calculate anchor space
  303.     Scroller.CalcAnchorSpace();
  304.  
  305.     //set wrap modes for scroller
  306.     Scroller.SetHWrapMode(WRAPMODE_CLIP);
  307.     Scroller.SetVWrapMode(WRAPMODE_CLIP);
  308.  
  309.     //set scroller anchor to (0,0)
  310.     Scroller.GetAnchor()->x=0;
  311.     Scroller.GetAnchor()->y=0;
  312.  
  313.     //attach scrolelr and tilewalker to mousemap
  314.     MouseMap.SetScroller(&Scroller);
  315.     MouseMap.SetTileWalker(&TileWalker);
  316.  
  317.     //set up the map to a random tilefield
  318.     for(int x=0;x<MAPWIDTH;x++)
  319.     {
  320.         for(int y=0;y<MAPHEIGHT;y++)
  321.         {
  322.             iMap[x][y]=rand()%2;
  323.         }
  324.     }
  325.  
  326.     //calculate the extent rect
  327.     RECT rcExtent;
  328.     CopyRect(&rcExtent,&tsBack.GetTileList()[0].rcDstExt);//set to background extent
  329.     UnionRect(&rcExtent,&rcExtent,&tsShadow.GetTileList()[0].rcDstExt);//union with shadow extent
  330.     UnionRect(&rcExtent,&rcExtent,&tsTree.GetTileList()[0].rcDstExt);//union with tree extent
  331.  
  332.     //set up the renderer
  333.     Renderer.SetBackBuffer(lpddsBack);
  334.     Renderer.SetExtentRect(&rcExtent);
  335.     Renderer.SetFrameBuffer(lpddsFrame);
  336.     Renderer.SetMapSize(MAPWIDTH,MAPHEIGHT);
  337.     Renderer.SetMouseMap(&MouseMap);
  338.     Renderer.SetPlotter(&TilePlotter);
  339.     Renderer.SetRenderFunction(RenderFunc);
  340.     Renderer.SetScroller(&Scroller);
  341.     Renderer.SetUpdateRectCount(100);
  342.     Renderer.SetWalker(&TileWalker);
  343.  
  344.     //update the entire screenspace
  345.     Renderer.AddRect(Scroller.GetScreenSpace());
  346.  
  347.     //set up the cursor
  348.     ptCursor.x=0;
  349.     ptCursor.y=0;
  350.  
  351.     return(true);//return success
  352. }
  353.  
  354. //////////////////////////////////////////////////////////////////////////////
  355. //CLEANUP
  356. //////////////////////////////////////////////////////////////////////////////
  357. void Prog_Done()
  358. {
  359.     //release frame buffer
  360.     LPDDS_Release(&lpddsFrame);
  361.  
  362.     //release main/back surfaces
  363.     LPDDS_Release(&lpddsMain);
  364.  
  365.     //release directdraw
  366.     LPDD_Release(&lpdd);
  367. }
  368.  
  369. //////////////////////////////////////////////////////////////////////////////
  370. //MAIN GAME LOOP
  371. //////////////////////////////////////////////////////////////////////////////
  372. void Prog_Loop()
  373. {
  374.     //grab the mouse position
  375.     POINT ptMouse;
  376.     GetCursorPos(&ptMouse);
  377.     //map the mouse
  378.     ptCursor=MouseMap.MapMouse(ptMouse);
  379.  
  380.     //clip the cursor to valid map coordinates
  381.     if(ptCursor.x<0) ptCursor.x=0;
  382.     if(ptCursor.y<0) ptCursor.y=0;
  383.     if(ptCursor.x>(MAPWIDTH-1)) ptCursor.x=MAPWIDTH-1;
  384.     if(ptCursor.y>(MAPHEIGHT-1)) ptCursor.y=MAPHEIGHT-1;
  385.  
  386.     //scroll the map
  387.     Renderer.ScrollFrame(ptScroll.x,ptScroll.y);
  388.  
  389.     //if a click was registered, add an update tile
  390.     if(bClick)
  391.     {
  392.         Renderer.AddTile(ptCursor.x,ptCursor.y);
  393.         iMap[ptCursor.x][ptCursor.y]=1-iMap[ptCursor.x][ptCursor.y];
  394.     }
  395.  
  396.     //set click to false
  397.     bClick=false;
  398.  
  399.     //update the frame
  400.     Renderer.UpdateFrame();
  401.  
  402.     //plot the cursor
  403.     POINT ptPlot;
  404.     ptPlot=TilePlotter.PlotTile(ptCursor);
  405.     ptPlot=Scroller.WorldToScreen(ptPlot);
  406.     tsCursor.ClipTile(lpddsBack,Scroller.GetScreenSpace(),ptPlot.x,ptPlot.y,0);
  407.  
  408.     //flip to show the back buffer
  409.     lpddsMain->Flip(0,DDFLIP_WAIT);
  410. }
  411.  
  412. void RenderFunc(LPDIRECTDRAWSURFACE7 lpddsDst,RECT* rcClip,int xDst,int yDst,int xMap,int yMap)
  413. {
  414.     //put background tile
  415.     tsBack.ClipTile(lpddsDst,rcClip,xDst,yDst,0);
  416.     //check for a tree
  417.     if(iMap[xMap][yMap])
  418.     {
  419.         //put shadow
  420.         tsShadow.ClipTile(lpddsDst,rcClip,xDst,yDst,0);
  421.         //put tree
  422.         tsTree.ClipTile(lpddsDst,rcClip,xDst,yDst,0);
  423.     }
  424. }
  425.